﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace hse_2013_project
{
    public partial class MainForm : Form
    {
        Random randFood = new Random();

        Graphics g;
        Snake snakes = new Snake();
        Food food;
        bool left = false;
        bool right = false;
        bool up = false;
        bool down = false;
        int score = 0;
        int speed=1;
        int [] highScore = new int[1];
        

        public MainForm()
        {
            InitializeComponent();
            food = new Food(randFood);
            label3.Visible = false;
        }

        /// <summary>
        /// Метод, который срабатывает при нарушении правил игры. Игра заканчивается
        /// </summary>
        public void Mistake()
        {
            for (int i = 1; i < snakes.SnakeRec.Length; i++)
            {
                if (snakes.SnakeRec[0].IntersectsWith(snakes.SnakeRec[i]))
                {
                    if (sound)
                    {
                        string str = "lose.mp3";
                        axWindowsMediaPlayer1.URL = @str;
                    }
                    Restart();
                }
            }
            if (snakes.SnakeRec[0].X < 0 || snakes.SnakeRec[0].X > 381)
            {
                if (sound)
                {
                    string str = "lose.mp3";
                    axWindowsMediaPlayer1.URL = @str;
                }
                Restart();

            }
            if (snakes.SnakeRec[0].Y < 24 || snakes.SnakeRec[0].Y > 414)
            {
                if (sound)
                {
                    string str = "lose.mp3";
                    axWindowsMediaPlayer1.URL = @str;
                }
                Restart();
            }



        }
        int k = 0;
        /// <summary>
        /// Метод, который останавливает игру, подсчитывает и обнуляет результаты
        /// </summary>
        public void Restart()
        {
            k = 0;
            timer1.Enabled = false;
            
            int max = highScore[0];
            if (score > max)
            {
                max = score;
                highScore[0] = max;
                if (sound)
                {
                    string str = "win.mp3";
                    axWindowsMediaPlayer1.URL = @str;
                }
                MessageBox.Show("ВЫ ПОБИЛИ РЕКОРД!");
                
            }
            MessageBox.Show("GAME OVER");
            StreamWriter sw = new StreamWriter("HIGH SCORE.txt", false, Encoding.Default);
            sw.Write(max.ToString());
            sw.Close();
            
            label2.Text = max.ToString();
            score = 0;
            toolStripStatusLabel2.Text = score.ToString();
            snakes = new Snake();


        }
        /// <summary>
        /// Метод управления змейкой различными клавишами
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyData == Keys.Space)
            {
                k++;
                label5.Visible = false;
                if (k == 1)
                {
                    timer1.Enabled = true;
                    down = false;
                    up = false;
                    left = false;
                    right = true;
                }
            }
            if (e.KeyData == Keys.Down && up == false)
            {
                down = true;
                up = false;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Up && down == false)
            {
                down = false;
                up = true;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Left && right == false)
            {
                down = false;
                up = false;
                right = false;
                left = true;
            }
            if (e.KeyData == Keys.Right && left == false)
            {
                down = false;
                up = false;
                right = true;
                left = false;
            }
            if (e.KeyData == Keys.P)
            {
                if (timer1.Enabled == false)
                {
                    label3.Visible = false;
                    timer1.Enabled = true;
                }
                else
                {
                    timer1.Enabled = false;
                    label3.Visible = true;

                }

              
            }
        }

       
        /// <summary>
        /// Метод отрисовки змеи и пищи
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            food.DrawFood(g);
            snakes.drawSnake(g);
        }

        /// <summary>
        /// Метод, отвечающий за направление движения, скорость и рост змеи
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (speed == 1)
            { timer1.Interval = 100; }
            if (speed == 2)
            { timer1.Interval = 75; }
            if (speed == 3)
            { timer1.Interval = 50; }
            if (down) { snakes.moveDown(); }
            if (up) { snakes.moveUp(); }
            if (right) { snakes.moveRight(); }
            if (left) { snakes.moveLeft(); }
            this.Invalidate();
            Mistake();
            for (int i = 0; i < snakes.SnakeRec.Length; i++)
            {
                if (snakes.SnakeRec[i].IntersectsWith(food.foodRec))
                {
                    
                    if (sound)
                    {
                        string str = "eat.mp3";
                        axWindowsMediaPlayer1.URL = @str;
 
                    }

                    score ++;
                    toolStripStatusLabel2.Text = score.ToString();
                    snakes.growSnake();
                    food.Location(randFood);

                }
            }
        }
        /// <summary>
        /// Выбран Легкий уровень
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void легкийToolStripMenuItem_Click(object sender, EventArgs e)
        {
            speed = 1;
        }

        /// <summary>
        /// Выбран Средний уровень
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void среднийToolStripMenuItem_Click(object sender, EventArgs e)
        {
            speed = 2;
        }

        /// <summary>
        /// Выбран Сложный уровень
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void сложныйToolStripMenuItem_Click(object sender, EventArgs e)
        {
            speed = 3;
        }

        /// <summary>
        /// Интструкция для игрока
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void инструкцииToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            Information info = new Information();
            info.ShowDialog();
        }

        /// <summary>
        /// Окно, на котором отображается лучший результат
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void статистикаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;

        }

   
        
        /// <summary>
        /// Скрытие статистики
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel1_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }
        /// <summary>
        /// Метод, срабатывающий при загрузке формы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Visible = false;
            panel1.Visible = false;
            StreamReader sr = new StreamReader("HIGH SCORE.txt", Encoding.Default);
            highScore[0] = int.Parse(sr.ReadLine());
            sr.Close();
            label2.Text = highScore[0].ToString();
        }
        /// <summary>
        /// Выход из программы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        bool sound = true;
        private void выключитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            sound = false;

        }

        private void включитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sound = true;
        }

        

      

       
       

    }
}
